import Utils from "./utils";
import CellObject from "./cell";
import GameConst from "./GameConst";
import FieldObject from "./FieldObject";


export default class LevelTarget {
    constructor() {
        this.scores = [500, 1500, 3e3];
        this.isScoreTarget = false;
        this.isBlockTarget = false;
        // this.progress = 0;
    }

    getExactType(e, t) {
        return -1;
    };
    getText() {
        return [];
    };
    onLevelGenerated() { };
    onMarkRemoved() { };
    onBlockRemoved() { };
    onObjectRemove(e) { };
    getTargetText() {
        return "";
    };
    onBushRemoved() { };

    isCompleted() {
        return this.progress >= 1 - 1e-10;
    };

}

export  class PushDownTarget extends LevelTarget {
    constructor(t) {
        super();
        this.targetsToPush = 0;
        this.targetsPushed = 0;
        this.pushCakeIn = 0;
        this.pushLeft = 0;
        this.pushQueue = 0;
        this.targetsToPush = t;
        this.setPushTime();
        
    }
    needToPush(){
        return this.pushLeft > 0;
    }
}





PushDownTarget.prototype.setPushTime = function () {
    this.pushCakeIn = GameConst.level.currentMove +Utils.getInt(3);
    if (this.needToPush()) {
        return;
    }
    this.pushLeft = (Math.random() < .02 && this.pushQueue + 2 <= this.targetsToPush) ? 2 : 1;
    this.pushQueue += this.pushLeft;
    if (this.pushQueue > this.targetsToPush)
        this.pushLeft = -1;
};

Object.defineProperty(PushDownTarget.prototype, "progress", {
    get: function () {
        return this.targetsPushed / this.targetsToPush;
    },
    enumerable: true,
    configurable: true
});
PushDownTarget.prototype.onTargetPush = function () {
    ++this.targetsPushed;
    if (!this.isCompleted())
        this.setPushTime();
};
PushDownTarget.prototype.getTargetText = function () {
    return "x" + Utils.limit(this.targetsToPush - this.targetsPushed, 0, 1e10);
};
PushDownTarget.prototype.getExactType = function (col, row) {
    if (row == 0 && !this.isCompleted() && this.needToPush() && GameConst.level.currentMove >= this.pushCakeIn && GameConst.levelData.pushPositions.indexOf(col) != -1) {
        this.pushLeft--;
        if (this.needToPush())
            this.setPushTime();
        return GameConst.level.assetNumber;
    }
    return -1;
};
PushDownTarget.prototype.getText = function () {
    return ["消除 " + this.targetsToPush , "个胡萝卜", "cupcake"];
};


export  class ClearMarkTarget extends LevelTarget {


    constructor() {
        super();
        this.marksToRemove = 1;
        this.marksRemoved = 0;
    }
}
Object.defineProperty(ClearMarkTarget.prototype, "progress", {
    get: function () {
        return this.marksRemoved / this.marksToRemove;
    },
    enumerable: true,
    configurable: true
});
ClearMarkTarget.prototype.getTargetText = function () {
    return "x" + Utils.limit(this.marksToRemove - this.marksRemoved, 0, 1e10);
};
ClearMarkTarget.prototype.onLevelGenerated = function () {
    this.marksToRemove = 0;
    for (var e = 0; e < GameConst.level.fieldWidth; ++e)
        for (var t = 0; t < GameConst.level.fieldHeight; ++t) {
            var n = GameConst.level.cells[e][t];
            if (n.isMarked)
                ++this.marksToRemove;
        }
};
ClearMarkTarget.prototype.getText = function () {
    return ["消除所有", "冰块!", "cookie"];
};
ClearMarkTarget.prototype.onMarkRemoved = function () {
    this.marksRemoved++;
};

export  class BlockClearTarget extends LevelTarget {
    constructor() {

        super();
        this.blocksToRemove = 1;
        this.blocksRemoved = 0;
        this.isBlockTarget = true;
    }
}

BlockClearTarget.prototype.getTargetText = function () {
    return "x" + Utils.limit(this.blocksToRemove - this.blocksRemoved, 0, 1e10);
};
BlockClearTarget.prototype.getText = function () {
    var e = GameConst.levelData;
    var t = e.hasBomb ? "chocolate_mine ico" : e.hasBlackChoco && e.hasWhiteChoco ? "chocolate ico" : e.hasWhiteChoco ? "chocolate white" : "chocolate black";
    return ["消除所有", "障碍物!", t];
};
Object.defineProperty(BlockClearTarget.prototype, "progress", {
    get: function () {
        return this.blocksRemoved / this.blocksToRemove;
    },
    enumerable: true,
    configurable: true
});
BlockClearTarget.prototype.onLevelGenerated = function () {
    this.blocksToRemove = 0;
    for (var e = 0; e < GameConst.level.fieldWidth; ++e)
        for (var t = 0; t < GameConst.level.fieldHeight; ++t) {
            var n = GameConst.level.cells[e][t];
            if ((n.isBreakable || n.isBombBlocked) && !n.isObjectBlocked)
                ++this.blocksToRemove;
        }
};
BlockClearTarget.prototype.onBlockRemoved = function () {
    this.blocksRemoved++;
};

export  class BushClearTarget extends LevelTarget {
    constructor() {
        super();
        this.bushToRemove = 1;
        this.bushRemoved = 0;
    }
}
BushClearTarget.prototype.getTargetText = function () {
    return "x" + Utils.limit(this.bushToRemove - this.bushRemoved, 0, 1e10);
};
BushClearTarget.prototype.getText = function () {
    return ["消除所有", "dangerous plants!", "bush"];
};
Object.defineProperty(BushClearTarget.prototype, "progress", {
    get: function () {
        return this.bushRemoved / this.bushToRemove;
    },
    enumerable: true,
    configurable: true
});
BushClearTarget.prototype.onLevelGenerated = function () {
    this.bushToRemove = 0;
    for (var e = 0; e < GameConst.level.fieldWidth; ++e)
        for (var t = 0; t < GameConst.level.fieldHeight; ++t) {
            var n = GameConst.level.cells[e][t];
            if (n.isPlantBlock)
                ++this.bushToRemove;
        }
};
BushClearTarget.prototype.onBushRemoved = function () {
    this.bushRemoved++;
};

export  class WaterClearTarget extends LevelTarget {
    constructor() {
        super();
    }
}
WaterClearTarget.prototype.getTargetText = function () {
    return "x" + CellObject.waterCells.length;
};
WaterClearTarget.prototype.getText = function () {
    return ["消除所有", "胶水!", "water"];
};
Object.defineProperty(WaterClearTarget.prototype, "progress", {
    get: function () {
        return CellObject.waterCells.length == 0 ? 1 : 0;
    },
    enumerable: true,
    configurable: true
});
WaterClearTarget.prototype.onLevelGenerated = function () { };

export  class ColorTarget extends LevelTarget {
    constructor(t, n) {
        super();
        this.colorsToRemove = 0;
        this.colorsRemoved = 0;
        this.colorType = 0;
        this.colorsToRemove = t;
        this.colorType = n;
    }
}

ColorTarget.prototype.getTargetText = function () {
    return "x" + Utils.limit(this.colorsToRemove - this.colorsRemoved, 0, 1e10);
};
ColorTarget.prototype.getText = function () {
    return ["消除 " + this.colorsToRemove + " 个" + FieldObject.targetNames[this.colorType], "果冻!", FieldObject.assetNames[this.colorType]];
};
Object.defineProperty(ColorTarget.prototype, "progress", {
    get: function () {
        return this.colorsRemoved / this.colorsToRemove;
    },
    enumerable: true,
    configurable: true
});
ColorTarget.prototype.onObjectRemove = function (e) {
    if (e.colorType == this.colorType)
        this.colorsRemoved++;
};


export  class ScoreTarget extends LevelTarget {
    constructor(t) {
        super();
        this.targetScore = 0;
        this.targetScore = t;
        this.isScoreTarget = true;
    }
}
ScoreTarget.prototype.getTargetText = function () {
    var e = Math.floor(100 * Utils.limit(GameConst.level.score / this.targetScore, 0, 1));
    return e + "%";
};
ScoreTarget.prototype.getText = function () {
    return ["获得 " + this.targetScore, "分!", "star"];
};
Object.defineProperty(ScoreTarget.prototype, "progress", {
    get: function () {
        return GameConst.level.score / this.targetScore;
    },
    enumerable: true,
    configurable: true
});


export  class InfiniteTarget extends LevelTarget {
    constructor() {
        super();
    }
}
InfiniteTarget.prototype.getTargetText = function () {
    return "xxx%";
};
InfiniteTarget.prototype.getText = function () {
    return ["获得 xxx", "分!", "star"];
};
Object.defineProperty(InfiniteTarget.prototype, "progress", {
    get: function () {
        return 0;
    },
    enumerable: true,
    configurable: true
});
